package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.crm.domain.pojo.CrmSearchPoint;
import com.yu.crm.domain.pojo.CrmSearchPointRoute;
import com.yu.crm.domain.pojo.CrmSearchRoute;
import com.yu.crm.mapper.CrmSearchPointRouteMapper;
import com.yu.crm.mapper.CrmSearchRouteMapper;
import com.yu.crm.service.ICrmSearchRouteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 巡更路线 服务实现类
 * </p>
 *
 * @author neo
 * @since 2020-07-22
 */
@Service
public class CrmSearchRouteService extends ServiceImpl<CrmSearchRouteMapper, CrmSearchRoute> implements ICrmSearchRouteService {
    @Override
    public void addOrUpdate(CrmSearchRoute crmSearchRoute) {
        if(DataHelpers.isNotEmpty(crmSearchRoute.getId())){
            crmSearchRoute.setGmtModified(LocalDateTime.now());
            this.updateById(crmSearchRoute);
        }else{
            crmSearchRoute.setGmtCreate(LocalDateTime.now());
            crmSearchRoute.setGmtModified(LocalDateTime.now());
            this.save(crmSearchRoute);
        }
    }


    @Override
    public IPage<CrmSearchRoute> pageAll(Page<CrmSearchRoute> page) {
        return this.pageAll(page);
    }


    @Override
    public List<CrmSearchPoint> listByRouteId(Long routeId) {
        return this.listByRouteId(routeId);
    }

    @Autowired
    CrmSearchPointRouteService crmSearchPointRouteService;
    public void removeById(Long id){

        //删除线路
        this.removeById(id);

        //删除线路相关的巡更点信息
        QueryWrapper<CrmSearchPointRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("route",id);
        crmSearchPointRouteService.remove(queryWrapper);


    }
}
